function getInformation(obj) { //向子页面传值
    document.getElementById("getInf").src="managerDetails.html?projectID="+obj.data.projectID;

}

//点击显示隐藏的页面
function getVisit(obj) {
    document.getElementById("getInf").style.display="block";
}

layui.extend({dtree:'../../DTree/src/main/resources/static/layui_ext/dtree/dtree'}).use(['dtree','form','layer','laydate','table','laytpl','tree','element','util'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table,
        element = layui.element,
        util = layui.util,
        dtree =layui.dtree

    //取得父页面url参数
    function getUrlParam(name) {//封装方法
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg); //匹配目标参数
        if (r != null) return unescape(r[2]);
        return null; //返回参数值
    }
    var nodeId =getUrlParam("nodeId");
    console.log(nodeId);
    var parentIdId =getUrlParam("parentId");
    console.log(parentIdId);
    var context =getUrlParam("context");
    console.log(context);
    var leaf =getUrlParam("leaf");
    console.log(leaf);
    var level =getUrlParam("level");
    console.log(level);
    var spread =getUrlParam("spread");
    console.log(spread);

    //新闻列表
    var tableIns = table.render({
        elem: '#newsList',
        url : '../../json/ProjectManagement.json?username='+nodeId+'&parentId='+parentIdId+'&context='
            +context+'&leaf='+leaf+'&level='+level+'&spread='+spread,
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limit : 20,
        limits : [10,15,20,25],
        id : "newsListTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'projectID', title: 'ID', width:60, align:"center"},
            {field: 'projectName', title: '项目名称', width:350},
            {field: 'projectCode', title: '编码', align:'center'},
            {field: 'projectAddress', title: '地点', align:'center'},
            {field: 'projectCompany', title: '公司', align:'center'},
            {field: 'projectDescribe', title: '项目描述', align:'center'},
            {field: 'projectStatus', title: '状态',  align:'center',templet:"#newsStatus"},
            {field: 'newsLook', title: '浏览权限', align:'center'},
            {field: 'newsTop', title: '是否置顶', align:'center', templet:function(d){
                    return '<input type="checkbox" name="newsTop" lay-filter="newsTop" lay-skin="switch" lay-text="是|否" '+d.newsTop+'>'
                }},
            {field: 'newsTime', title: '发布时间', align:'center', minWidth:110, templet:function(d){
                    return d.newsTime.substring(0,10);
                }},
            {title: '操作', width:170, templet:'#newsListBar',fixed:"right",align:"center"}
        ]]
    });

    //是否置顶
    form.on('switch(newsTop)', function(data){
        var index = layer.msg('修改中，请稍候',{icon: 16,time:false,shade:0.8});
        setTimeout(function(){
            layer.close(index);
            if(data.elem.checked){
                layer.msg("置顶成功！");
            }else{
                layer.msg("取消置顶成功！");
            }
        },500);
    })

    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").on("click",function(){
        if($(".searchVal").val() != ''){
            table.reload("newsListTable",{
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    key: $(".searchVal").val()  //搜索的关键字
                }
            })
        }else{
            layer.msg("请输入搜索的内容");
        }
    });

    //添加文章
    function addNews(edit){
        var index = layui.layer.open({
            title : "添加文章",
            type : 2,
            content : "newsAdd.html",
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                if(edit){
                    body.find(".projectName").val(edit.projectName);
                    body.find(".abstract").val(edit.abstract);
                    body.find(".thumbImg").attr("src",edit.newsImg);
                    body.find("#news_content").val(edit.content);
                    body.find(".projectStatus select").val(edit.newsStatus);
                    body.find(".openness input[name='openness'][title='"+edit.newsLook+"']").prop("checked","checked");
                    body.find(".newsTop input[name='newsTop']").prop("checked",edit.newsTop);
                    form.render();
                }
                setTimeout(function(){
                    layui.layer.tips('点击此处返回文章列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        })
    }
    $(".addNews_btn").click(function(){
        addNews();
    })

    //批量删除
    $(".delAll_btn").click(function(){
        var checkStatus = table.checkStatus('newsListTable'),
            data = checkStatus.data,
            projectID = [];
        if(data.length > 0) {
            for (var i in data) {
                projectID.push(data[i].newsId);
            }
            layer.confirm('确定删除选中的文章？', {icon: 3, title: '提示信息'}, function (index) {
                // $.get("删除文章接口",{
                //     newsId : newsId  //将需要删除的newsId作为参数传入
                // },function(data){
                tableIns.reload();
                layer.close(index);
                // })
            })
        }else{
            layer.msg("请选择需要删除的文章");
        }
    })

    //列表操作
    table.on('tool(newsList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'edit'){ //编辑
            addNews(data);
        } else if(layEvent === 'del'){ //删除
            layer.confirm('确定删除此文章？',{icon:3, title:'提示信息'},function(index){
                // $.get("删除文章接口",{
                //     newsId : data.newsId  //将需要删除的newsId作为参数传入
                // },function(data){
                tableIns.reload();
                layer.close(index);
                // })
            });
        } else if(layEvent === 'look'){ //预览

            // layer.alert("此功能需要前台展示，实际开发中传入对应的必要参数进行文章内容页面访问")
            $(this).on("click",function(){
                table.reload('newsListTable', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        url:'../json/newListTest.json',
                        key: {
                            key:''//将 input的name属性为 id 的值传给后台
                        }
                    }
                }, 'data');
            });

        }
    });

    //监听行单击事件
    table.on('row(newsList)', function(obj){
        console.log(obj.tr) //得到当前行元素对象
        console.log(obj.data) //得到当前行数据
        //obj.del(); //删除当前行
        //obj.update(fields) //修改当前行数据
        getVisit(obj);
        getInformation(obj);//根据projectID请求相应的详情页
    });

//监听行双击事件
    table.on('rowDouble(test)', function(obj){
        //obj 同上
    });

})
